package sourismobile.server.core;

import java.io.DataInputStream;
import java.io.DataOutputStream;

import sourismobile.server.module.SourisMobileMousse;
import sourismobile.server.module.SourisMobilePresenter;

public class ServiceConnector implements Runnable{

	DataInputStream  in;
	DataOutputStream out;
	int moduleType = 0;
	boolean end = false;

	Module module;
	BluetoothServer server;


	public ServiceConnector(DataOutputStream out, DataInputStream in, int moduleType, BluetoothServer server) {

		this.moduleType = moduleType;

		switch (this.moduleType) {
		case 1:
			module = new SourisMobilePresenter();	
			System.out.println("Presenter running !");
			break;
		case 2:
			module = new SourisMobileMousse();
			System.out.println("Mousse running !");
			break;
		}

		this.in = in;
		this.out = out;
		this.server = server;

	}
	public void run() {

		int keyCode; 
		
		try {
			while (!end) {
				if(in.available()>1) {
					keyCode = in.readInt();
					System.out.println("\n- " + keyCode + " -");
					
					if (keyCode == 0) {
						return;
					}
					
					module.action(keyCode);
				}
				else {System.out.print(".");}
				Thread.sleep(100);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
